﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using SAAC.DTO;
using System.Data;

namespace SAAC.DAL
{
	public class ParecerDAL : DALBase
	{

		public ParecerDTO Select(ParecerDTO dto, string order = null)
		{
			try
			{
				string sql = "CALL sp_parecer_sel(@descricao,@criacao,@numeroProcesso,@comissaoId,@order,@limit);";

				if (dto.Processo == null) dto.Processo = new ProcessoDTO();
				if (dto.Comissao == null) dto.Comissao = new ComissaoDTO();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@descricao", dto.Descricao);
				cmd.Parameters.AddWithValue("@criacao", dto.Criacao);
				cmd.Parameters.AddWithValue("@numeroProcesso", dto.Processo.Numero);
				cmd.Parameters.AddWithValue("@comissaoId", dto.Comissao.Id);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", 1);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				if (dr.Read())
				{
					dto.Descricao = (dr["descricao"]).ToString();
					dto.Criacao = Convert.ToDateTime(dr["nome"]);
					dto.Processo.Numero = (dr["Processo_numero"]).ToString();
					dto.Comissao.Id = Convert.ToInt32(dr["Comissao_idComissao"]);
				}
				else dto = null;

				return dto;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public List<ParecerDTO> SelectAll(ParecerDTO dto, string order = null, int limit = 1000)
		{
			try
			{
				string sql = "CALL sp_parecer_sel(@descricao,@criacao,@numeroProcesso,@comissaoId,@order,@limit);";

				if (dto.Processo == null) dto.Processo = new ProcessoDTO();
				if (dto.Comissao == null) dto.Comissao = new ComissaoDTO();
				var lista = new List<ParecerDTO>();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@descricao", dto.Descricao);
				cmd.Parameters.AddWithValue("@criacao", dto.Criacao);
				cmd.Parameters.AddWithValue("@numeroProcesso", dto.Processo.Numero);
				cmd.Parameters.AddWithValue("@comissaoId", dto.Comissao.Id);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", limit);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				while (dr.Read())
				{
					var item = new ParecerDTO();
					item.Descricao = (dr["descricao"]).ToString();
					item.Criacao = Convert.ToDateTime(dr["nome"]);
					item.Processo.Numero = (dr["Processo_numero"]).ToString();
					item.Comissao.Id = Convert.ToInt32(dr["Comissao_idComissao"]);
					lista.Add(item);
				}

				return lista;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Insert(ParecerDTO dto)
		{
			try
			{
				String sql = @"CALL sp_parecer_ins(@descricao,@criacao,@numeroProcesso,@comissaoId);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@descricao", dto.Descricao);
				cmd.Parameters.AddWithValue("@criacao", dto.Criacao);
				cmd.Parameters.AddWithValue("@numeroProcesso", dto.Processo.Numero);
				cmd.Parameters.AddWithValue("@comissaoId", dto.Comissao.Id);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Update(ParecerDTO dto)
		{
			try
			{
				String sql = "CALL sp_parecer_upd(@descricao,@criacao,@numeroProcesso,@comissaoId);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@descricao", dto.Descricao);
				cmd.Parameters.AddWithValue("@criacao", dto.Criacao);
				cmd.Parameters.AddWithValue("@numeroProcesso", dto.Processo.Numero);
				cmd.Parameters.AddWithValue("@comissaoId", dto.Comissao.Id);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Delete(ParecerDTO dto)
		{
			try
			{
				String sql = "CALL up_parecer_del(@numeroProcesso,@comissaoId);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@numeroProcesso", dto.Processo.Numero);
				cmd.Parameters.AddWithValue("@comissaoId", dto.Comissao.Id);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

	}
}
